package cheqex.update;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import cheqex.dao.CollectionCash;
import cheqex.dao.CollectionCashPeer;
import cheqex.dao.CollectionCheck;
import cheqex.dao.CollectionCheckPeer;
import cheqex.update.controllers.DateController;

public class CollectionView extends JPanel implements ActionListener{
	private JSplitPane splitPaneCollection;
	private JScrollPane depositCashScrollPane;
	private JScrollPane depositCheckScrollPane;
	
	private JTable cashTable;
	private JTable checkTable;
	
	private JButton addCash;
	private JButton addCheck;
	private JButton deleteCash;
	private JButton deleteCheck;
	private JButton editCash;
	private JButton editCheck;
	private JLabel dSep1;
	private JLabel dSep2;
	
	public CollectionView(){
		initialize();
	}
	
	public void populateTable(){
		this.cashTable.setModel(new DefaultTableModel(getCashData(), getCashNames()));
		this.checkTable.setModel(new DefaultTableModel(getCheckData(), getCheckNames()));
	}
	
	private String[][] getCashData(){
		List<CollectionCash> list = CollectionCashPeer.retrieveAllCollectionCash(DateController.getDate());
		String[][] data = new String[list.size()][3];
		for(int i=0; i<list.size(); i++){
			CollectionCash dc = list.get(i);
			data[i][0] = "" + dc.getORNumber();
			data[i][1] = "" + dc.getAmount();
			data[i][2] = "" + dc.getParticulars();
		}
		return data;
	}
	
	private String[][] getCheckData(){
		List<CollectionCheck> list = CollectionCheckPeer.retrieveAllCollectionCheck(DateController.getDate());
		String[][] data = new String[list.size()][3];
		for(int i=0; i<list.size(); i++){
			CollectionCheck dc = list.get(i);
			data[i][0] = "" + dc.getPRNumber();
			data[i][1] = "" + dc.getAmount();
			data[i][2] = "" + dc.getParticulars();
		}
		return data;
	}
	
	private String[] getCashNames(){
		return new String[]
			{"OR Number", "Amount", "Particulars"};
	}
	
	private String[] getCheckNames(){
		return new String[]
			{"PR Number", "Amount", "Particulars"};
	}
	
	public void initialize(){
		this.cashTable = new JTable();
		this.cashTable.setFillsViewportHeight(true);
		this.cashTable.setShowGrid(true);
		this.cashTable.setOpaque(true);
	    this.cashTable.setAutoResizeMode(5);
	    this.cashTable.setRowHeight(24);
	    this.cashTable.setEnabled(false);
	    
	    
		this.checkTable = new JTable();
		this.checkTable.setFillsViewportHeight(true);
		this.checkTable.setShowGrid(true);
		this.checkTable.setOpaque(true);
	    this.checkTable.setAutoResizeMode(5);
		this.checkTable.setRowHeight(24);
		this.checkTable.setEnabled(false);
		
		
		depositCashScrollPane = new JScrollPane(this.cashTable);
		depositCheckScrollPane = new JScrollPane(this.checkTable);
		
		populateTable();
		this.cashTable.getTableHeader().setReorderingAllowed(false);
		this.checkTable.getTableHeader().setReorderingAllowed(false);
		
		addCash = new JButton("Add+");
		addCheck = new JButton("Add+");
		deleteCash = new JButton("Delete-");
		deleteCheck = new JButton("Delete-");
		editCash = new JButton("Edit");
		editCheck = new JButton("Edit");
		dSep1 = new JLabel(" Collection Cash > ");
		dSep2 = new JLabel(" < Collection Check  ");
		
	    //buttons
	    addCash.setMnemonic(KeyEvent.VK_D);
	    addCash.setActionCommand("addCash");
	    addCash.setEnabled(true);
	    addCash.addActionListener(this);
	    
	    editCash.setMnemonic(KeyEvent.VK_D);
	    editCash.setActionCommand("editCash");
	    editCash.setEnabled(true);
	    editCash.addActionListener(this);
	    
	    deleteCash.setMnemonic(KeyEvent.VK_D);
	    deleteCash.setActionCommand("deleteCash");
	    deleteCash.setEnabled(true);
	    deleteCash.addActionListener(this);
	    
	    addCheck.setMnemonic(KeyEvent.VK_D);
	    addCheck.setActionCommand("addCheck");
	    addCheck.setEnabled(true);
	    addCheck.addActionListener(this);
	    
	    editCheck.setMnemonic(KeyEvent.VK_D);
	    editCheck.setActionCommand("editCheck");
	    editCheck.setEnabled(true);
	    editCheck.addActionListener(this);
	    
	    deleteCheck.setMnemonic(KeyEvent.VK_D);
	    deleteCheck.setActionCommand("deleteCheck");
	    deleteCheck.setEnabled(true);
	    deleteCheck.addActionListener(this);
		
		splitPaneCollection = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, depositCashScrollPane, depositCheckScrollPane);
		splitPaneCollection.setOneTouchExpandable(false);
		splitPaneCollection.setDividerLocation(300);
		splitPaneCollection.setAutoscrolls(false);
		
		Dimension minimumSize = new Dimension(100, 100);					
        	depositCashScrollPane.setMinimumSize(minimumSize);	        
        	depositCheckScrollPane.setMinimumSize(minimumSize);	      
        splitPaneCollection.setPreferredSize(new Dimension(600, 300));

        
        JPanel jp = new JPanel();
        jp.setLayout(new GridLayout(4,0,4,0));
        jp.add(dSep1);
        jp.add(addCash);
		jp.add(editCash);
		jp.add(deleteCash);
		jp.setSize(new Dimension(10,10));
		
		JPanel jl = new JPanel();
		jl.setLayout(new GridLayout(4,0,4,0));
		jl.add(dSep2);
		jl.add(addCheck);
		jl.add(editCheck);
		jl.add(deleteCheck);
		jl.setSize(new Dimension(50,80));
		
		this.add(jp);
		this.add(splitPaneCollection);
		this.add(jl);
		
		this.setAutoscrolls(true);
		this.setEnabled(true);
		this.setLayout(new FlowLayout(FlowLayout.LEFT));
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if ("addCash".equals(e.getActionCommand())) {
			AddCollectionCashView add = new AddCollectionCashView(this);
			add.setVisible(true);
		}
		
		if ("addCheck".equals(e.getActionCommand())) {
			AddCollectionCheckView add = new AddCollectionCheckView(this);
			add.setVisible(true);	
		}
		
		if ("editCash".equals(e.getActionCommand())) {
			EditCollectionCashView edit = new EditCollectionCashView(this);
			edit.setVisible(true);
		}
		
		if ("editCheck".equals(e.getActionCommand())) {
			EditCollectionCheckView edit = new EditCollectionCheckView(this);
			edit.setVisible(true);
		}
		
		if ("deleteCash".equals(e.getActionCommand())) {
			DeleteCollectionCashView delete = new DeleteCollectionCashView(this);
			delete.setVisible(true);
		}
		
		if ("deleteCheck".equals(e.getActionCommand())) {
			DeleteCollectionCheckView delete = new DeleteCollectionCheckView(this);
			delete.setVisible(true);
		}
	}
}
